package com.ansion.commonchat.startup.workmanager.bwork;

import androidx.annotation.NonNull;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * Created by wanghongjie on 2023/5/5
 * Describe :  包含了一个Work的各种信息。它有以下属性：
 * <p>
 * mId: UUID类型，表示Work的唯一标识符。
 * mState: State类型，表示Work的当前状态，包括ENQUEUED（已加入队列）、RUNNING（正在运行）、SUCCEEDED（运行成功）、FAILED（运行失败）、BLOCKED（被阻塞）等。
 * mOutputData: Data类型，表示Work运行结束后的输出数据。
 * mTags: Set<String>类型，表示Work关联的标签集合。
 */
public final class BWorkInfo {

    private @NonNull
    UUID mId;
    private @NonNull
    State mState;
    private @NonNull
    BData mOutputBData;
    private @NonNull
    Set<String> mTags;

    /**
     * @hide
     */
    public BWorkInfo(
            @NonNull UUID id,
            @NonNull State state,
            @NonNull BData outputBData,
            @NonNull List<String> tags) {
        mId = id;
        mState = state;
        mOutputBData = outputBData;
        mTags = new HashSet<>(tags);
    }

    public @NonNull
    UUID getId() {
        return mId;
    }

    public @NonNull
    State getState() {
        return mState;
    }

    public @NonNull
    BData getOutputBData() {
        return mOutputBData;
    }

    public @NonNull
    Set<String> getTags() {
        return mTags;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        BWorkInfo workInfo = (BWorkInfo) o;

        if (!mId.equals(workInfo.mId)) return false;
        if (mState != workInfo.mState) return false;
        if (!mOutputBData.equals(workInfo.mOutputBData)) return false;
        return !mTags.equals(workInfo.mTags);
    }

    @Override
    public int hashCode() {
        int result = mId.hashCode();
        result = 31 * result + mState.hashCode();
        result = 31 * result + mOutputBData.hashCode();
        result = 31 * result + mTags.hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "BWorkInfo{"
                + "mId='" + mId + '\''
                + ", mState=" + mState
                + ", mOutputBData=" + mOutputBData
                + ", mTags=" + mTags
                + '}';
    }

    /**
     * State类型，表示Work的当前状态，
     * 包括ENQUEUED（已加入队列）、RUNNING（正在运行）、SUCCEEDED（运行成功）、
     * FAILED（运行失败）、BLOCKED（被阻塞）、CANCELLED（被取消）
     */
    public enum State {
        ENQUEUED,
        RUNNING,
        SUCCEEDED,
        FAILED,
        BLOCKED,
        CANCELLED;

        public boolean isFinished() {
            return (this == SUCCEEDED || this == FAILED || this == CANCELLED);
        }
    }
}